package com.wuji1626.spark.rdd.transform

import org.apache.spark.{SparkConf, SparkContext}

object RDD_Transform_mapPartitionsWithIndex_Sample {

  def main(args: Array[String]): Unit = {
    // Step1: 准备环境
    val sparkConf = new SparkConf().setMaster("local[*]").setAppName("Operator")
    val sc = new SparkContext(sparkConf)

    // Step2: 算子 mapPartitionsWithIndex
    // 构建分区【1,2】【3,4】
//    val rdd = sc.makeRDD(List(1, 2, 3, 4),numSlices = 2)
    val rdd = sc.makeRDD(List(1, 2, 3, 4))


    val mp = rdd.mapPartitionsWithIndex(
      (index, iter) => {
        iter.map(
          num => (index, num)
        )
      }
    )
    mp.collect().foreach(println)
    // Step3: 关闭环境
    sc.stop()
  }

}
